<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>core/EventManager.js - Documentation</title>

    <script src="scripts/prettify/prettify.js"></script>
    <script src="scripts/prettify/lang-css.js"></script>
    <!--[if lt IE 9]>
      <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <link type="text/css" rel="stylesheet" href="https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css">
    <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
    <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>

<input type="checkbox" id="nav-trigger" class="nav-trigger" />
<label for="nav-trigger" class="navicon-button x">
  <div class="navicon"></div>
</label>

<label for="nav-trigger" class="overlay"></label>

<nav>
    <li class="nav-link nav-home-link"><a href="index.html">Home</a></li><li class="nav-heading">Classes</li><li class="nav-heading"><span class="nav-item-type type-class">C</span><span class="nav-item-name"><a href="Constants.html">Constants</a></span></li><li class="nav-heading"><span class="nav-item-type type-class">C</span><span class="nav-item-name"><a href="Extension.html">Extension</a></span></li><li class="nav-heading"><span class="nav-item-type type-class">C</span><span class="nav-item-name"><a href="ExtensionAction.html">ExtensionAction</a></span></li><li class="nav-heading"><span class="nav-item-type type-class">C</span><span class="nav-item-name"><a href="FrameworkState.html">FrameworkState</a></span></li><li class="nav-heading"><span class="nav-item-type type-class">C</span><span class="nav-item-name"><a href="Minima.html">Minima</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Minima.html#.addService">addService</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Minima.html#.getDefaultService">getDefaultService</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Minima.html#.getExtensions">getExtensions</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Minima.html#.getPlugin">getPlugin</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Minima.html#.getPlugin">getPlugin</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Minima.html#.getPlugins">getPlugins</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Minima.html#.getPlugins">getPlugins</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Minima.html#.getServices">getServices</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Minima.html#.removeService">removeService</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Minima.html#.start">start</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Minima.html#.stop">stop</a></span></li><li class="nav-heading"><span class="nav-item-type type-class">C</span><span class="nav-item-name"><a href="Plugin.html">Plugin</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Plugin.html#.assertResolved">assertResolved</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Plugin.html#.assertUninstalled">assertUninstalled</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Plugin.html#.doStart">doStart</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Plugin.html#.doStop">doStop</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Plugin.html#.loadClass">loadClass</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Plugin.html#.resolveActivator">resolveActivator</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Plugin.html#.start">start</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Plugin.html#.stop">stop</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Plugin.html#.uninstall">uninstall</a></span></li><li class="nav-heading"><span class="nav-item-type type-class">C</span><span class="nav-item-name"><a href="PluginConfiguration.html">PluginConfiguration</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="PluginConfiguration.html#.parseBasic">parseBasic</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="PluginConfiguration.html#.parseDependencies">parseDependencies</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="PluginConfiguration.html#.parseExtensions">parseExtensions</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="PluginConfiguration.html#.parseServices">parseServices</a></span></li><li class="nav-heading"><span class="nav-item-type type-class">C</span><span class="nav-item-name"><a href="PluginContext.html">PluginContext</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="PluginContext.html#.addExtension">addExtension</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="PluginContext.html#.addExtensionChangedListener">addExtensionChangedListener</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="PluginContext.html#.addExtensionChangedListener">addExtensionChangedListener</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="PluginContext.html#.addFrameworkStateChangedListener">addFrameworkStateChangedListener</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="PluginContext.html#.addPluginStateChangedListener">addPluginStateChangedListener</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="PluginContext.html#.addPluginStateChangedListener">addPluginStateChangedListener</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="PluginContext.html#.addService">addService</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="PluginContext.html#.addServiceChangedListener">addServiceChangedListener</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="PluginContext.html#.addServiceChangedListener">addServiceChangedListener</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="PluginContext.html#.dispose">dispose</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="PluginContext.html#.getDefaultService">getDefaultService</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="PluginContext.html#.getExtensions">getExtensions</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="PluginContext.html#.getServices">getServices</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="PluginContext.html#.installPlugin">installPlugin</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="PluginContext.html#.removeExtension">removeExtension</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="PluginContext.html#.removeExtensionChangedListener">removeExtensionChangedListener</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="PluginContext.html#.removeExtensionChangedListener">removeExtensionChangedListener</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="PluginContext.html#.removeFrameworkStateChangedListener">removeFrameworkStateChangedListener</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="PluginContext.html#.removePluginStateChangedListener">removePluginStateChangedListener</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="PluginContext.html#.removePluginStateChangedListener">removePluginStateChangedListener</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="PluginContext.html#.removeService">removeService</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="PluginContext.html#.removeServiceChangedListener">removeServiceChangedListener</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="PluginContext.html#.removeServiceChangedListener">removeServiceChangedListener</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="PluginContext.html#.start">start</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="PluginContext.html#.stop">stop</a></span></li><li class="nav-heading"><span class="nav-item-type type-class">C</span><span class="nav-item-name"><a href="PluginMetadata.html">PluginMetadata</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="PluginMetadata.html#.markFailed">markFailed</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="PluginMetadata.html#.markResolvable">markResolvable</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="PluginMetadata.html#.markSuccess">markSuccess</a></span></li><li class="nav-heading"><span class="nav-item-type type-class">C</span><span class="nav-item-name"><a href="PluginState.html">PluginState</a></span></li><li class="nav-heading"><span class="nav-item-type type-class">C</span><span class="nav-item-name"><a href="ResolveState.html">ResolveState</a></span></li><li class="nav-heading"><span class="nav-item-type type-class">C</span><span class="nav-item-name"><a href="ServiceAction.html">ServiceAction</a></span></li><li class="nav-heading"><span class="nav-item-type type-class">C</span><span class="nav-item-name"><a href="ServiceRegistry.html">ServiceRegistry</a></span></li><li class="nav-heading"><span class="nav-item-type type-class">C</span><span class="nav-item-name"><a href="Version.html">Version</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Version.html#.compare">compare</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Version.html#.parse">parse</a></span></li>
</nav>

<div id="main">
    
    <h1 class="page-title">core/EventManager.js</h1>
    

    



    
    <section>
        <article>
            <pre class="prettyprint source linenums"><code>import Extension from '../Extension';
import ExtensionAction from '../ExtensionAction';
import Framework from './Framework';
import FrameworkState from './FrameworkState';
import PluginState from '../PluginState';
import ServiceAction from '../ServiceAction';
import log from '../utility/log';

/**
 * 事件管理器，用于监听、取消监听、触发事件
 * 
 * @ignore
 * @export
 * @class EventManager
 */
export default class EventManager {
    /**
     * 创建一个事件管理器
     * 
     * @ignore
     * @memberof EventManager
     */
    constructor() {
        this.frameworkStateChangedListeners = new Set();
        this.pluginStateChangedListeners = new Set();
        this.serviceChangedListeners = new Set();
        this.extensionChangedListeners = new Set();

        this.dispose = this.dispose.bind(this);

        this.addPluginStateChangedListener = this.addPluginStateChangedListener.bind(this);
        this.firePluginStateChanged = this.firePluginStateChanged.bind(this);
        this.removePluginStateChangedListener = this.removePluginStateChangedListener.bind(this);

        this.addFrameworkStateChangedListener = this.addFrameworkStateChangedListener.bind(this);
        this.fireFrameworkStateChanged = this.fireFrameworkStateChanged.bind(this);
        this.removeFrameworkStateChangedListener = this.removeFrameworkStateChangedListener.bind(this);

        this.addServiceChangedListener = this.addServiceChangedListener.bind(this);
        this.fireServiceChanged = this.fireServiceChanged.bind(this);
        this.removeServiceChangedListener = this.removeServiceChangedListener.bind(this);

        this.addExtensionChangedListener = this.addExtensionChangedListener.bind(this);
        this.fireExtensionChanged = this.fireExtensionChanged.bind(this);
        this.removeExtensionChangedListener = this.removeExtensionChangedListener.bind(this);
    }

    /**
     * 清理资源
     * 
     * @memberof EventManager
     */
    dispose() {
        this.frameworkStateChangedListeners.clear();
        this.pluginStateChangedListeners.clear();
        this.serviceChangedListeners.clear();
        this.extensionChangedListeners.clear();
    }

    /**
     * 添加框架状态监听器
     * 
     * @param {function} listener 监听器函数
     * @example 
     * frameworkStateChangedListener(framework, previousState, currentState) {
     *     // ...
     * }
     * @memberof EventManager
     */
    addFrameworkStateChangedListener(listener) {
        if (!this.frameworkStateChangedListeners.has(listener)) {
            this.frameworkStateChangedListeners.add(listener);
        }
    }

    /**
     * 删除框架状态监听器
     * 
     * @param {function} listener 监听器函数
     * @memberof EventManager
     */
    removeFrameworkStateChangedListener(listener) {
        if (this.frameworkStateChangedListeners.has(listener)) {
            this.frameworkStateChangedListeners.delete(listener);
        }
    }

    /**
     * 触发框架状态变更
     * 
     * @param {Framework} framework 框架实例
     * @param {FrameworkState} previousState 前一个状态
     * @param {FrameworkState} currentState 当前状态
     * @memberof EventManager
     */
    fireFrameworkStateChanged(framework, previousState, currentState) {
        for (let listener of this.frameworkStateChangedListeners) {
            try {
                listener(framework, previousState, currentState);
            } catch (error) {
                log.logger.error('Failed to invoke the frameworkStateChanged listener.', error);
            }
        }
    }

    /**
     * 添加插件状态监听器
     * 
     * @param {function} listener 监听器函数
     * @example
     * pluginStateChangedListener(id, previous, current) {
     *     // ...
     * }
     * @memberof EventManager
     */
    addPluginStateChangedListener(listener) {
        if (!this.pluginStateChangedListeners.has(listener)) {
            this.pluginStateChangedListeners.add(listener);
        }
    }

    /**
     * 删除插件状态监听器
     * 
     * @param {function} listener 监听器函数 
     * @memberof EventManager
     */
    removePluginStateChangedListener(listener) {
        if (this.pluginStateChangedListeners.has(listener)) {
            this.pluginStateChangedListeners.delete(listener);
        }
    }

    /**
     * 触发插件状态变更
     * 
     * @param {string} id 插件Id
     * @param {PluginState} previousState 前一个状态
     * @param {PluginState} currentState 当前状态
     * @memberof EventManager
     */
    firePluginStateChanged(id, previousState, currentState) {
        for (let listener of this.pluginStateChangedListeners) {
            try {
                listener(id, previousState, currentState);
            } catch (error) {
                log.logger.error('Failed to invoke the pluginStateChanged listener.', error);
            }
        }
    }

    /**
     * 添加服务变更监听器
     * 
     * @param {function} listener 监听器函数
     * @example 
     * serviceChangedListener(name, action) {
     * }
     * @memberof EventManager
     */
    addServiceChangedListener(listener) {
        if (!this.serviceChangedListeners.has(listener)) {
            this.serviceChangedListeners.add(listener);
        }
    }

    /**
     * 删除服务变更监听器
     * 
     * @param {function} listener 监听器函数
     * @memberof EventManager
     */
    removeServiceChangedListener(listener) {
        if (this.serviceChangedListeners.has(listener)) {
            this.serviceChangedListeners.delete(listener);
        }
    }

    /**
     * 触发服务变更事件
     * 
     * @param {string} name 服务名称
     * @param {ServiceAction} action 变化活动
     * @memberof EventManager
     */
    fireServiceChanged(name, action) {
        for (let listener of this.serviceChangedListeners) {
            try {
                listener(name, action);
            } catch (error) {
                log.logger.error('Failed to invoke the serviceChanged listener.', error);
            }
        }
    }

    /**
     * 添加扩展变更监听器
     * 
     * @param {function} listener 监听器函数
     * @example
     * extensionChangedListener(extension, action) {
     *     // ......
     * }
     * @memberof EventManager
     */
    addExtensionChangedListener(listener) {
        if (!this.extensionChangedListeners.has(listener)) {
            this.extensionChangedListeners.add(listener);
        }
    }

    /**
     * 删除扩展变更监听器
     * 
     * @param {function} listener 监听器函数
     * @memberof EventManager
     */
    removeExtensionChangedListener(listener) {
        if (this.extensionChangedListeners.has(listener)) {
            this.extensionChangedListeners.delete(listener);
        }
    }

    /**
     * 触发扩展变更时间
     * 
     * @param {Extension} extension 扩展对象
     * @param {ExtensionAction} action 变化活动
     * @memberof EventManager
     */
    fireExtensionChanged(extension, action) {
        for (let listener of this.extensionChangedListeners) {
            try {
                listener(extension, action);
            } catch (error) {
                log.logger.error('Failed to invoke the extensionChanged listener.', error);
            }
        }
    }
}</code></pre>
        </article>
    </section>




</div>

<br class="clear">

<footer>
    Generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Wed Oct 18 2017 23:16:27 GMT+0800 (CST) using the Minami theme.
</footer>

<script>prettyPrint();</script>
<script src="scripts/linenumber.js"></script>
</body>
</html>
